package com.jndi.context;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;

public class JndiGetContext {
	public Context ctx;

	public Context getContext() {
		Context ctx = null;
		Properties env = new Properties();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL, "ldap://localhost:10389");
		env.put(Context.SECURITY_PRINCIPAL,"uid=admin,ou=system");
		env.put(Context.SECURITY_CREDENTIALS, "secret");
		env.put(Context.REFERRAL, "follow");
		env.put(Context.SECURITY_AUTHENTICATION, "simple");  
		//{com.sun.jndi.ldap.connect.timeout=500, 
		//com.sun.jndi.ldap.connect.pool=true, 
		//com.sun.jndi.ldap.read.timeout=15000}
		env.put("com.sun.jndi.ldap.connect.timeout", "500");
		env.put("com.sun.jndi.ldap.connect.pool","true");
		env.put("com.sun.jndi.ldap.read.timeout", "15000");
		
		try {
			ctx = new InitialLdapContext(env, null);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ctx;
	}

	public Context getCtx() {
		return ctx;
	}

	public void setCtx(Context ctx) {
		this.ctx = ctx;
	}

}
